package com.alphacsp.theedge2012.ui.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.alphacsp.theedge2012.R;
import com.alphacsp.theedge2012.data.database.DataAccessor;
import com.alphacsp.theedge2012.data.entities.Event;
import com.alphacsp.theedge2012.data.entities.Session;
import com.alphacsp.theedge2012.data.service.NetworkService;
import com.alphacsp.theedge2012.data.service.NetworkServiceHelper;
import com.alphacsp.theedge2012.data.service.ServiceListener;
import com.alphacsp.theedge2012.ui.components.calendar.CalendarEventButton;
import com.alphacsp.theedge2012.ui.components.calendar.CalendarLayout;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 23/10/10
 * Time: 14:32
 */
public class ScheduleActivity extends Activity implements ServiceListener {

    private static final int DISABLED_BLOCK_ALPHA = 200;
    private DataAccessor dataAccessor;
    private CalendarLayout calendarLayout;
    private NetworkServiceHelper networkServiceHelper;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schedule_activity);
        dataAccessor = DataAccessor.getSingleton(this);
        initCalendar();
        networkServiceHelper = new NetworkServiceHelper(this);
        final Intent serviceIntent = new Intent(Intent.ACTION_SYNC, null, this, NetworkService.class);
        serviceIntent.putExtra(NetworkService.STATUS_LISTENER, networkServiceHelper);
        serviceIntent.putExtra(NetworkService.REFRESH_ACTION, NetworkService.REFRESH_SCHEDULE);
        startService(serviceIntent);
    }

    private void initCalendar() {
        calendarLayout = (CalendarLayout) findViewById(R.id.calendar_layout);
        final List<Event> events = dataAccessor.getAllEvents();
        for (final Event event : events) {
            final CalendarEventButton calendarEventButton = new CalendarEventButton(this, event, null);
            LayerDrawable buttonDrawable = (LayerDrawable) calendarEventButton.getBackground();
            Log.w("ScheduleActivity", "Event:  " + event.getName());

            buttonDrawable.getDrawable(0).setAlpha(DISABLED_BLOCK_ALPHA);
            buttonDrawable.getDrawable(1).setAlpha(DISABLED_BLOCK_ALPHA);
            calendarLayout.addEvent(calendarEventButton);
            calendarEventButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    final Session session = dataAccessor.getSession(event.getName());
                    if (session != null) {
                        Intent intent = new Intent(ScheduleActivity.this, SessionDetailsActivity.class);
                        intent.putExtra("sessionTopic", session.getTopic());
                        intent.putExtra("sessionPresenter", session.getPresenter());
                        intent.putExtra("sessionAbstract", session.getSessionAbstract());
                        startActivity(intent);
                    } else {
                        Toast toast = Toast.makeText(getApplicationContext(), "No details are available", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
            });
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        dataAccessor.closeDatabase();
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onHomeClick(View view) {
        final Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onSearchClick(View v) {
        onSearchRequested();
    }

    public void onReceiveResult(int resultCode, Bundle resultData) {
        if (resultCode == NetworkService.STATUS_REFRESHED) {
            calendarLayout.removeAllViews();
            initCalendar();
        }
    }
}
